package com.diapoc.androidteacher.PathMenu;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.Canvas;
import android.graphics.Matrix;
import android.graphics.Paint;
import android.util.AttributeSet;
import android.util.Log;
import android.view.MotionEvent;
import android.view.View;
import android.view.View.OnClickListener;
import android.view.View.OnTouchListener;

import com.diapoc.androidteacher.R;
import com.diapoc.androidteacher.utils.LogManager;

public class PathMenu extends View implements OnTouchListener, OnClickListener {

	public float height = 0;
	public float width = 0;
	public int height_mode;
	public int width_mode;
	private Context context;

	public PathMenu(Context context) {
		super(context);
	}

	public PathMenu(Context context, AttributeSet attrs) {
		super(context, attrs);
	}

	public PathMenu(Context context, AttributeSet attrs, int defStyle) {
		super(context, attrs, defStyle);
	}

	@Override
	protected void onDraw(Canvas canvas) {
		super.onDraw(canvas);
		drawCanvas(canvas);
	}

	@Override
	protected void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
		super.onMeasure(widthMeasureSpec, heightMeasureSpec);
		width = MeasureSpec.getSize(widthMeasureSpec);
		height = MeasureSpec.getSize(heightMeasureSpec);
		width_mode = MeasureSpec.getMode(widthMeasureSpec);
		height_mode = MeasureSpec.getMode(heightMeasureSpec);
		init(context);
	}

	@Override
	public void onClick(View v) {
		click(v);
	}

	@Override
	public boolean onTouch(View v, MotionEvent event) {
		return false;
	}

	public void init(Context context) {
		this.context = context;
		initCanvas();
		setOnClickListener(this);
		Log.v("test", "init");
	}

	// 下面开始是自定义的界面部分
	Paint paint;
	private Bitmap button;
	private Bitmap plus;
	private Bitmap camera;
	private Bitmap place;
	private Bitmap sleep;
	private Bitmap thought;
	private Bitmap with;

	private boolean rotateThreadTag = false;
	private boolean expandThreadTag = false;
	private float btnsRotate = 0;
	// 按钮占整个控件的比例的倒数
	private float mainBtn_rate = 3f;
	// 按钮中+占整个控件的比例的倒数
	private float plus_rate;
	// 其他按钮占整个空间的比例的倒数
	private float btns_rate;
	// 主按钮旋转角度
	private int mainBtnRotate = 0;
	// 是否在展开状态
	private boolean isExpanded = false;
	// 展开比例,单位signal
	private float expandRate = 135;
	// 展开总耗时，单位signal
	private float expandTotal = 135f;
	// 功能按钮和主按钮的偏移量，即半径的差距
	float translate_function2button;
	// 展开半径
	float radius;
	// 功能按钮数量
	float btn_num = 5;
	// 各按钮偏移角度
	float btn_angle;
	// 每个功能按钮展开的时间，单位signal
	float singleSignal = 50;
	// 每个功能按钮展开的时间间隔，单位signal
	float singleSignalPlus;

	/**
	 * 自定义画图初始化
	 */
	private void initCanvas() {
		initPaint();
		initBitmap();
		initParameter();
	}

	/**
	 * 初始化画笔
	 */
	private void initPaint() {
		paint = new Paint();
	}

	/**
	 * 初始化从位图
	 */
	private void initBitmap() {
		button = BitmapFactory.decodeResource(getResources(),
				R.drawable.pathmenu_button);
		plus = BitmapFactory.decodeResource(getResources(),
				R.drawable.pathmenu_plus);
		camera = BitmapFactory.decodeResource(getResources(),
				R.drawable.pathmenu_camera);
		place = BitmapFactory.decodeResource(getResources(),
				R.drawable.pathmenu_place);
		place = BitmapFactory.decodeResource(getResources(),
				R.drawable.pathmenu_place);
		sleep = BitmapFactory.decodeResource(getResources(),
				R.drawable.pathmenu_sleep);
		thought = BitmapFactory.decodeResource(getResources(),
				R.drawable.pathmenu_thought);
		with = BitmapFactory.decodeResource(getResources(),
				R.drawable.pathmenu_with);
	}

	private void initParameter() {
		plus_rate = mainBtn_rate * button.getWidth() / plus.getWidth();
		btns_rate = mainBtn_rate * button.getWidth() / camera.getWidth();
		translate_function2button = (width / mainBtn_rate - width / btns_rate) / 2;
		radius = (height * (mainBtn_rate - 1) / mainBtn_rate + translate_function2button);
		btn_angle = (float) (Math.PI / ((btn_num - 1) * 2));
		singleSignalPlus = (expandTotal - singleSignal) / (btn_num - 1);
	}

	/**
	 * 自定义画布
	 * 
	 * @param canvas
	 */
	private void drawCanvas(Canvas canvas) {
		drawFunctionBtns(canvas);
		drawMainBtn(canvas);
	}

	/**
	 * 画主按钮
	 * 
	 * @param canvas
	 */
	private void drawMainBtn(Canvas canvas) {
		float button_center_x = width / (plus_rate * 2);
		float button_center_y = height / (plus_rate * 2);
		// 主按钮
		Matrix matrix_button = new Matrix();
		matrix_button.postScale(width / (button.getWidth() * mainBtn_rate),
				height / (button.getHeight() * mainBtn_rate));
		matrix_button.postTranslate(0, height * (mainBtn_rate - 1)
				/ mainBtn_rate);
		canvas.drawBitmap(button, matrix_button, paint);
		// 主按钮中的+
		Matrix matrix_plus = new Matrix();
		float translate_plus2button = (width / mainBtn_rate - width / plus_rate) / 2;
		matrix_plus.postScale(width / (plus.getWidth() * plus_rate), height
				/ (plus.getHeight() * plus_rate));
		matrix_plus.postRotate(mainBtnRotate, button_center_x, button_center_y);
		matrix_plus.postTranslate(translate_plus2button, height
				* (mainBtn_rate - 1) / mainBtn_rate + translate_plus2button);
		canvas.drawBitmap(plus, matrix_plus, paint);
	}

	/**
	 * 画功能按钮
	 * 
	 * @param canvas
	 */
	private void drawFunctionBtns(Canvas canvas) {
		drawFunctionBtn(canvas, 0, camera);
		drawFunctionBtn(canvas, 1, place);
		drawFunctionBtn(canvas, 2, sleep);
		drawFunctionBtn(canvas, 3, thought);
		drawFunctionBtn(canvas, 4, with);
	}

	private void drawFunctionBtn(Canvas canvas, int id, Bitmap bitmap) {
		if (expandRate > singleSignalPlus * (4 - id)
				&& expandRate < singleSignal + singleSignalPlus * (4 - id)) {
			LogManager.showLog("test" + id, "     " + expandRate + "     "
					+ (expandRate - singleSignalPlus * (4 - id)) + "/"
					+ singleSignal);
			Matrix matrix = new Matrix();
			matrix.postScale(width / (with.getWidth() * btns_rate), height
					/ (with.getHeight() * btns_rate));
			matrix.postTranslate(
					(float) (translate_function2button + radius
							* Math.sin(btn_angle * id)
							* (1f - (expandRate - singleSignalPlus * (4 - id))
									/ singleSignal)),
					(float) (radius * (1f - Math.cos(btn_angle * id) + Math
							.cos(btn_angle * id)
							* ((expandRate - singleSignalPlus * (4 - id)) / singleSignal))));
			canvas.drawBitmap(bitmap, matrix, paint);

		} else if (expandRate <= singleSignalPlus * (4 - id)) {
			if (!rotateThreadTag) {
				Matrix matrix = new Matrix();
				matrix.postScale(width / (with.getWidth() * btns_rate), height
						/ (with.getHeight() * btns_rate));
				matrix.postTranslate(
						(float) (translate_function2button + radius
								* Math.sin(btn_angle * id) * 1f),
						(float) (radius * (1f - Math.cos(btn_angle * id) + Math
								.cos(btn_angle * id) * 0f)));
				canvas.drawBitmap(bitmap, matrix, paint);
			}
		} else if (expandRate >= singleSignal + singleSignalPlus * (4 - id)) {
			Matrix matrix = new Matrix();
			matrix.postScale(width / (with.getWidth() * btns_rate), height
					/ (with.getHeight() * btns_rate));
			matrix.postTranslate((float) (translate_function2button + radius
					* Math.sin(btn_angle * id) * 0f),
					(float) (radius * (1f - Math.cos(btn_angle * id) + Math
							.cos(btn_angle * id) * 1f)));
			canvas.drawBitmap(bitmap, matrix, paint);
		}
		if (rotateThreadTag && isExpanded) {
			Matrix matrix = new Matrix();
			LogManager.showLog("便宜" + translate_function2button);
			matrix.postRotate(btnsRotate,
					(button.getWidth() - translate_function2button * 2) / 2,
					(button.getHeight() - translate_function2button * 2) / 2);
			matrix.postScale(width / (with.getWidth() * btns_rate), height
					/ (with.getHeight() * btns_rate));
			matrix.postTranslate((float) (translate_function2button + radius
					* Math.sin(btn_angle * id) * 1f),
					(float) (radius * (1f - Math.cos(btn_angle * id) + Math
							.cos(btn_angle * id) * 0f)));
			canvas.drawBitmap(bitmap, matrix, paint);
		}
	}

	/**
	 * 自定义点击
	 */
	private void click(View v) {
		new Thread() {
			public void run() {
				while (btnsRotate < 360 && isExpanded) {
					rotateThreadTag = true;
					btnsRotate += 2.5f;
					try {
						sleep(1);
					} catch (InterruptedException e) {
						// TODO Auto-generated catch block
						e.printStackTrace();
					}
					postInvalidate();
				}
				btnsRotate = 0;
				rotateThreadTag = false;
				new Thread() {
					int signal = 0;

					public void run() {
						while (signal < expandTotal) {
							rotatePlus(signal, 1);
							expand(signal, 1);
							signal++;
							try {
								sleep(1);
							} catch (InterruptedException e) {
								// TODO Auto-generated catch block
								e.printStackTrace();
							}
						}
						isExpanded = !isExpanded;
					}
				}.start();
			}
		}.start();

	}

	/**
	 * 旋转加号+按钮
	 * 
	 * @param signal
	 * @param frequency
	 */
	private void rotatePlus(int signal, int frequency) {
		// LogManager.showLog(signal + "   " + start + "  " + end + "  "
		// + frequency);
		if (signal % frequency == 0) {
			if (!isExpanded) {
				mainBtnRotate = mainBtnRotate - 1;
				postInvalidate();
			} else {
				mainBtnRotate = mainBtnRotate + 1;
				postInvalidate();
			}
		}
	}

	/**
	 * 展开
	 * 
	 * @param signal
	 * @param frequency
	 */
	private void expand(int signal, int frequency) {
		if (signal % (frequency) == 0) {
			if (!isExpanded) {
				expandRate--;
			} else {
				expandRate++;
			}
			postInvalidate();
		}
	}
}
